<?php

namespace App\depository;

use App\Models\PlayerSecretModel;

class PlayerSecretDep
{
    public $model;

    public function __construct()
    {
        $this->model = new PlayerSecretModel();
    }

    public function firstByName($name){

        $res = $this->model->where('name',$name)->first();
        return $res;

    }

    public function first($id){
        return $this->model->where('id', $id)->first();
    }

    public function list($teamId,$param)
    {
        $res = $this->model
            ->where('team_id', $teamId)
            ->when(!empty($param['start_at']), function($query) use ($param) {
                $query->where("start_at", ">=", $param['start_at'][0]);
                $query->where("start_at", "<=", $param['start_at'][1]);
            })
            ->when(!empty($param['end_at']), function($query) use ($param) {
                $query->where("end_at", ">=", $param['end_at'][0]);
                $query->where("end_at", "<=", $param['end_at'][1]);
            })
            ->when(!empty($param['id']), function ($query) use ($param) {
                $query->where("id",$param['id']);
            })
            ->when(!empty($param['steamid']), function($query) use ($param){
                $query->where('steamid','like' ,"%{$param['steamid']}%");
            })
            ->when(!empty($param['group']), function($query) use ($param){
                $query->where('group','like' ,"%{$param['group']}%");
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;

        return $res;
    }


    public function add($data)
    {
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function upd($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->delete();
        return $res;
    }

    public function firstBySteamId($teamId,$steamid){

        $res = $this->model->where('steamid',$steamid)->where('team_id',$teamId)->first();
        return $res;

    }

    public function getBySteamId($steamid){

        $res = $this->model->where('steamid',$steamid)->get();
        return $res;

    }

    public function getByTeamId($teamId){

        $res = $this->model->where('team_id',$teamId)->get();
        return $res;

    }

    public function getExp(){
        $res = $this->model->where('end_at','<=',date('Y-m-d H:i:s'))->get();
        return $res;
    }

}
